﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OptimumInfra.ViewData;

namespace OptimumInfra.Models
{
    public class ServiceRepository
    {
        private OptimumDataContext op = new OptimumDataContext();
        #region service
        public void CreateServiceAttribute(int supportId, int amount, int price)
        {
            Attribute attribute = new OptimumInfra.Models.Attribute();
            attribute.Amount = amount;
            attribute.SupportId = supportId;
            attribute.Price = price;
            if (!IsAttributeExisted(2, supportId, amount))
            {
                op.Attributes.InsertOnSubmit(attribute);
                op.SubmitChanges();
            }
        }

        public IEnumerable<ViewDataSupportServices> GetAllServices()
        {
            var result = from r in op.SupportServices
                         select new { r.Id, r.Item, r.Description, r.Unit };

            List<ViewDataSupportServices> t = new List<ViewDataSupportServices>();
            foreach (var r in result)
            {
                ViewDataSupportServices temp = new ViewDataSupportServices();
                temp.Description = r.Description;
                temp.Unit = r.Unit;
                temp.PricePerAmount = ServicePricePerAmount((int)r.Id);
                temp.Name = r.Item;
                temp.Id = r.Id;
                t.Add(temp);

            }
            return t;
        }

        public Dictionary<int, string> ServicePricePerAmount(int itemId)
        {
            Dictionary<int,string> t = new Dictionary<int, string>();
            var result = from r in op.SupportServices
                         join a in op.Attributes on r.Id equals a.SupportId

                         where r.Id == itemId
                         select new { r.Id, a.Amount, a.Price };
            foreach (var r in result)
            {
                string price = r.Price.ToString() + "_" + r.Id.ToString();
                t.Add((int)r.Amount, price);
            }
            return t;
        }
        public Dictionary<int, int> ServicePrices(int itemId)
        {
            Dictionary<int, int> t = new Dictionary<int, int>();
            var result = from r in op.SupportServices
                         join a in op.Attributes on r.Id equals a.SupportId

                         where r.Id == itemId
                         select new { r.Id, a.Amount, a.Price };
            foreach (var r in result)
            {
                t.Add((int)r.Amount, (int)r.Price);
            }
            return t;
        }

        public SupportService CreateService(SupportService r)
        {
            op.SupportServices.InsertOnSubmit(r);
            op.SubmitChanges();
            return r;
        }

        public void UpdateService(SupportService r)
        {
            var service = op.SupportServices.Single(a => a.Id == r.Id);
            service.Item = r.Item;
            service.Description = r.Description;
            service.Unit = r.Unit;
            op.SubmitChanges();
        }

        public ViewDataSupportServices GetViewDataServiceById(int id)
        {
            var result = op.SupportServices.SingleOrDefault(a => a.Id == id);
            ViewDataSupportServices resource = new ViewDataSupportServices();
            resource.Id = result.Id;
            resource.Name = result.Item;
            resource.Description = result.Description;
            resource.Unit = result.Unit;
            resource.Prices = ServicePrices(id);
            return resource;
        }

        public SupportService GetServiceById(int id)
        {
            return op.SupportServices.SingleOrDefault(a => a.Id == id);
         
        }

        public void DeleteServiceById(SupportService r)
        {
            op.SupportServices.DeleteOnSubmit(r);
            op.SubmitChanges();
        }

        public void DeleteAttributeById(Attribute r)
        {

            op.Attributes.DeleteOnSubmit(r);
            op.SubmitChanges();
        }
        public Attribute GetAttributeById(int id)
        {

            return op.Attributes.Single(a => a.Id == id);
        }

        public Attribute GetResourceAttributeByAmount(int resourceId, int amount)
        {

            var r = from a in op.Attributes where a.Amount == amount select a;
            Attribute attribute = new Attribute();
            foreach (Attribute a in r)
            {
                attribute = a;
                break;
            }
            return attribute;

        }
        public bool IsAttributeExisted(int type, int typeId, int amount)
        {
            if (type == 1)
            {
                return this.op.Attributes.Any(r => r.Amount == amount && r.ResourceId == typeId);
            }
            else
            {
                return this.op.Attributes.Any(r => r.Amount == amount && r.SupportId == typeId);
            }
        }
        public Attribute GetAttributeByAmount(int amount, int type)
        {

            return op.Attributes.Single(a => a.Amount == amount && a.SupportId == type);
        }
        #endregion

    }
}